<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * User Application Controller: Member / User
 * Started: September 3, 2009 17:29 PM
 * Script version: 1.0
 */
function __autoload($void){
	$obj = (isset($_SESSION["cf_controller"]) ? unserialize($_SESSION["cf_controller"]) : false);
	if($obj == false) die();
	require_once(BASE_PATH."applications/universal/user/controllers/group.php"); # Wtf again?
	if(!class_exists("group")) trigger_error("Group System not found");
	if($obj->status !== false){
		$obj->group = new group($obj->user['group'], false);
		if($obj->group->getStatus() === false){
			trigger_error("User {$this->user['username']}'s Group {$this->user['group']} does not exist. Layers broken, operation aborted");
		}
	}
}
class user{
	public function __construct($identifier, $type = 'uid'){
		global $db;
		if(strcmp($type, "uid") == 0){
			$this->uid = intval($identifier);
			$query = $db->select("users", "*", "1", " WHERE `id` = {$this->uid}");
		}
		elseif(strcmp($type, "username") == 0){
			$this->username = mysql_real_escape_string(stripslashes($identifier));
			$query = $db->select("users", "*", "1", " WHERE `username` = '{$this->username}'");
		}
		
		if($query){
			if($db->num_rows($query) == 0){
				$this->status = false;
			}
			else{
				$this->user = $db->fetch_array($query);
				$this->status = true;
			}
		}
		else{
			$this->status = false;
		}
		//---------------------------------------
		// Group, group, groupy!
		//---------------------------------------
		if($this->status !== false){
			$this->group = new group($this->user['group'], false);
			if($this->group->getStatus() === false){
				trigger_error("User {$this->user['username']}'s Group {$this->user['group']} does not exist. Layers broken, operation aborted");
			}
			$this->groupArray = $this->group->group;
		}
	}
	
	public function getStatus(){
		return $this->status;
	}
	
	public function getUsername(){
		return $this->user['username'];
	}
	
	public function getDisplayName(){
		return $this->user['display'];
	}
	
	public function getUID(){
		return $this->user['id'];
	}
	
	public function getPassHash(){
		return $this->user['password'];
	}
	
	public function getEmail(){
		return $this->user['email'];
	}
	
	public function getGroupID(){
		return $this->user['group'];
	}
	
	public function delete(){
		global $db;
		$query = $db->query("DELETE FROM `".DB_PREFIX."users` WHERE `id` = {$this->uid} LIMIT 1");
		return $query;
	}
}
